from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from six_it.models import Recruit


class CollectionRecruitView(APIView):
    '''收藏职位'''
    permission_classes = [IsAuthenticated]

    def post(self, request, id):
        recruit = Recruit.objects.get(id=id)
        user = request.user
        recruit.users.add(user)
        recruit.save()
        return Response({
            "message": "收藏成功",
            "success": True
        })


class CancelCollectRecruitView(APIView):
    '''取消收藏职位'''
    permission_classes = [IsAuthenticated]

    def post(self, request, id):
        recruit = Recruit.objects.get(id=id)
        user = request.user
        recruit.users.remove(user)
        recruit.save()
        return Response({
            "message": "取消收藏",
            "success": True
        })
